package com.pay.service.impl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pay.constant.Constant;
import com.pay.container.ZfbRobtoContainer;
import com.pay.container.robto.ZfbRobto;
import com.pay.entity.ZfbMember;
import com.pay.mapper.ZfbMemberMapper;
import com.pay.service.IZfbMemberService;
import com.pay.utils.BasePage;
import com.pay.utils.ResponseData;
import com.pay.utils.ZfbRobtoUtils;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Peng
 * @since 2018-12-05
 */
@Service
public class ZfbMemberServiceImpl extends ServiceImpl<ZfbMemberMapper, ZfbMember> implements IZfbMemberService {

	private Lock lock = new ReentrantLock() ;
	
	@Override
	public void getList(ZfbMember zfbMember, BasePage<ZfbMember> page) {
		QueryWrapper<ZfbMember> ew = new QueryWrapper<ZfbMember>();
		
		if(page.isPage()) {
			//分页
			super.page(page, ew) ;
		}else {
			//不分页
			List<ZfbMember> data = super.list(ew) ;
			int total = super.count(ew);
			page.setRecords(data) ;
			page.setTotal(total) ;
		}
	}

	@Override
	public ResponseData doZfbRobtoAdd(ZfbMember zfbMember) {
		if(StrUtil.isBlank(zfbMember.getCookie())) {
			return ResponseData.baseError("cookie不能为空") ;
		}
		if(StrUtil.isBlank(zfbMember.getMark())) {
			return ResponseData.baseError("备注不能为空") ;
		}
		boolean result = super.save(zfbMember);
		return result ? ResponseData.ok() : ResponseData.baseError("添加机器人失败") ;
	}

	@Override
	public ResponseData doZfbRobtoEdit(ZfbMember zfbMember) {
		if(StrUtil.isBlank(zfbMember.getCookie())) {
			return ResponseData.baseError("cookie不能为空") ;
		}
		if(StrUtil.isBlank(zfbMember.getMark())) {
			return ResponseData.baseError("备注不能为空") ;
		}
		boolean result = super.updateById(zfbMember);
		return result ? ResponseData.ok() : ResponseData.baseError("修改机器人失败") ;
	}

	@Override
	public ResponseData doZfbSwitch(Integer id, Integer status) {
		try {
			lock.lock();
			ZfbMember zfbMember = super.getById(id);
			if(zfbMember == null) {
				return ResponseData.baseError("启动异常");
			}
			if(status == Constant.robto_on_line) {
				//在线操作
				if(ZfbRobtoContainer.container.get(zfbMember.getId()) != null) {
					return ResponseData.baseError("机器人已经在线") ;
				}
				String cookie = zfbMember.getCookie();
				String ctoken = ZfbRobtoUtils.getCToken(cookie);
				String userId = ZfbRobtoUtils.getUserId(cookie);
				zfbMember.setCtoken(ctoken);
				zfbMember.setUserId(userId);
				zfbMember.setStatus(Constant.robto_on_line);  //设置机器人在线
				zfbMember.setCreatetime(new Date());
				boolean result = super.updateById(zfbMember) ;
				if(!result) {
					return ResponseData.baseError("操作失败") ;
				}
				ZfbRobto robto = new ZfbRobto();
				robto.setZfbMember(zfbMember);  //将信息保存给机器人
				ZfbRobtoContainer.container.put(zfbMember.getId(), robto) ; //保存到容器
				new Thread(robto).start(); ;
			}else {
				//离线操作
				ZfbRobto robto = ZfbRobtoContainer.container.get(zfbMember.getId());
				if(robto != null) {
					robto.getZfbMember().setStatus(Constant.robto_off_line); //将机器人设置为离线状态
					ZfbRobtoContainer.container.remove(zfbMember.getId()) ; //将容器中移除
				}
				
				zfbMember.setStatus(Constant.robto_off_line); 
				boolean result = super.updateById(zfbMember); //操作成功
				return result ? ResponseData.ok() : ResponseData.baseError("操作失败") ;
			}
		}catch(Exception e) {
			throw e ; 
		}finally {
			lock.unlock(); 
		}
		
		return ResponseData.ok() ;
	}

	/**
	 * 随机获取一个队公机器人
	 */
	@Override
	public ZfbMember getRandOnline() {
		QueryWrapper<ZfbMember> ew = new QueryWrapper<ZfbMember>();
		ew.eq("status", Constant.robto_on_line) ; //在线的机器人
		ew.eq("model", Constant.robto_to_public) ;
		ew.orderByDesc("RAND()") ; // 随机函数
		ZfbMember zfbMember = super.getOne(ew);
		return zfbMember ; 
	}

	@Override
	public ZfbMember getPrivateRobto(Integer zfbMemberId) {
		QueryWrapper<ZfbMember> ew = new QueryWrapper<ZfbMember>();
		ew.eq("status", Constant.robto_on_line) ; //在线的机器人
		ew.eq("model", Constant.robto_to_private) ;
		ew.eq("id", zfbMemberId) ;
		ZfbMember zfbMember = super.getOne(ew);
		return zfbMember ; 
	}

}
